package com.dmg.service.impl;

import com.dmg.config.DataSourceContextHolder;
import com.dmg.dao.BiaoMapper;
import com.dmg.entity.Biao;
import com.dmg.service.BiaoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
public class BiaoServiceImpl implements BiaoService {

    @Autowired
    private BiaoMapper biaoMapper;

   // @MyDataSource("zh01")
    @Override
    public void aa() {
        String zuHuId = DataSourceContextHolder.getDataSourceKey();
        log.info("aa-当前租户id:{}",zuHuId);
        Biao b = biaoMapper.getInfo();
        log.info("查询数据库id:{},name:{}",b.getId(),b.getName());
    }

    /**
     * 修改事务的传播行为 使得多数据源下 事务注解能够生效
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addBiao() {
        String zuHuId = DataSourceContextHolder.getDataSourceKey();
        log.info("进入添加biao方法,当前租户id:{}",zuHuId);
        Biao b = new Biao();
        b.setName("王五");
        biaoMapper.insert(b);
        //测试 多数据源场景下 事务是否会回滚
        // int a= 1/0;
    }
}
